<?php

namespace app\common\model;

use app\distributor\model\Distributor;
use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 * 分销商商品模型
 */
class DistributorGoods Extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    // 表名
    protected $name = 'distributor_goods';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $markup_sum = 0;//所有上级加价和

    /**
     * @param int $value
     * 设置所有上级加价和
     */
    public function setMarkup($value = 0)
    {
        $this->markup_sum = $value;
    }
    /***
     * @param $parent_id 父级id
     * @param $goods_id 商品id
     * @param int $sum
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 分销商所有上级加价之和
     */
    public function parent_markup_price_sum($parent_id,$goods_id)
    {
        if ($parent_id){
            $where = [
                'distributor_id' => $parent_id,
                'goods_id' => $goods_id
            ];
            $goods = $this->where($where)->find();
            $this->markup_sum += $goods['markup_price'];
            $dist = Distributor::get($parent_id);
            $this->parent_markup_price_sum($dist['parent_id'],$goods_id);
        }
        return $this->markup_sum;
    }

    /***
     * @param int $sales_cost 销售成本
     * @param int $markup 进行配置的加价或采购的加价、商品的配置加价+采购价之和
     * @param int $parent_markup 父级的配置加价
     * @return int 该分销商的成
     */
    public function cost_price($sales_cost = 0,$markup = 0, $parent_markup = 0)
    {
        return $sales_cost + $markup + $parent_markup;
    }


    /***
     * @param int $dist_id 分销商id
     * @param int $goods_id 商品id
     * @param float $sales_cost 销售方案成本价
     * @return int
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 分销商小店商品成本价
     */
    public function shop(int $dist_id, int $goods_id,$sales_cost = 0)
    {   
        $dist = Distributor::get($dist_id);
        $where = [
            'goods_id' => $goods_id,
            'distributor_id' => $dist_id,
        ];
        $goods = $this->get($where);
        $purchase_markup = $this->procurement($goods['id']);//采购价格
        $parent_markup = $this->parent_markup_price_sum($dist['parent_id'],$goods_id);//父级的加价之和
        $markup = $goods['markup_price'] + $purchase_markup;
        $cost_price = $this->cost_price($sales_cost,$markup,$parent_markup);
        return $cost_price;
    }

    /**
     * [addGoods 分销添加商品]
     * @Author   ZhouYunpeng
     * @DateTime 2021-09-25
     * @param    array       $params   [description]
     * @param    [type]      $dis_id   [description]
     * @param    [type]      $goods_id [description]
     */
    public function addGoods($params = [], $user_id, $goods_id)
    {
        
        $goods = self::withTrashed()->where(['goods_id' => $goods_id,'user_id' => $user_id])->find();

        Db::startTrans();
        try {
            if ($goods) {
                //存在
                if ($goods['delete_time']){
                    $params['delete_time'] = null;
                }
                $params['update_time'] = time();
                 $goods->allowField(true)->save($params);
            } else {
                $params['create_time'] = time();
                self::create($params,true);
            }
            Db::commit();

        } catch (Exception $e) {
            Db::rollback();
            throw new Exception($e->getMessage());
        }

    }


    
}
